<?php
declare (strict_types=1);

namespace app\common\model;

class BusinessApply extends Base
{

    protected $name = 'business_apply';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,account,code,nickname,mobile');
    }

    public function p()
    {
        return $this->hasOne(Area::class, 'code', 'province_id')->field('code,province as title');
    }

    public function c()
    {
        return $this->hasOne(Area::class, 'code', 'city_id')->field('code,city as title');
    }

    public function d()
    {
        return $this->hasOne(Area::class, 'code', 'district_id')->field('code,district as title');
    }

    public function getStatusAttr($value)
    {
        switch ($value) {
            case 1:
                return "待审核";
            case 2:
                return "已通过";
            default:
                return "已驳回";
        }
    }

    public function getLicenseAttr($value)
    {
        return $value ? explode(',', $value) : "";
    }

    public static function _save($param, $admin): array
    {
        parent::startTrans();
        try {
            $info = parent::where('id', $param['id'])->find();
            if (empty($info)) {
                return ['code' => -1, 'msg' => '记录不存在'];
            }
            if ($info->getData('status') != 1) {
                return ['code' => -1, 'msg' => '记录已审核，请勿重复审核'];
            }

            if ($param['type'] == 2) {
                unset($info->id);
                unset($info->status);
                unset($info->create_time);
                unset($info->update_time);
                $newData = $info->toArray();
                $newData['license'] = implode(',', $newData['license']);
                $new = Business::create($newData);
                User::update(['business_id' => $new->id], ['id' => $info->uid]);
            }
            $info->reject_msg = $param['reject_msg'];
            $info->status = $param['type'];
            $info->admin_id = $admin->id;
            $info->admin_name = $admin->admin_name;
            $info->audit_time = time();
            $info->save();
            parent::commit();
            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }


    public static function add($param, $user): array
    {
        try {
            /*if($user->syn_level < 3) {
                return ['code' => -1, 'msg' => '身份不符，无权申请商家！'];
            }*/
            $count = parent::where('uid', $user->id)->where('status', '<', 3)->count();
            if ($count) {
                return ['code' => -1, 'msg' => '您已是商家或已经提交审核，请勿重复提交！'];
            }
            if ($param['type'] == 2) {
                if (!isset($param['district_id'])) {
                    return ['code' => -1, 'msg' => '地址错误！'];
                }
                $address = Area::where('code', $param['district_id'])->find();
                if (empty($address)) {
                    return ['code' => -1, 'msg' => '地址错误！'];
                }
                $param['full_address'] = $address->province . $address->city . $address->district . $param['address_details'];
            }

            $param['uid'] = $user->id;
            parent::create($param);

            return ['code' => 1, 'msg' => '申请已提交，请耐心等待审核！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
